// Manutenzione
var Manutenzione = new Class( {
	options: {
		handler:'/administration/core/xml_server.asmx'
	},						 
	elementi: {
		str_operazione  : 'manu_type',
		str_action  : 'actionPane',		
		str_title  : 'title_',				
		str_corpo  : 'corpo_'			
	},
	
	live : {
		contenitore : null
		
	}, 
	initialize: function(options) {
		this.options.handler = options.handler;
		//azzera visus:		
		this.live.contenitore = $(this.elementi.str_action); 
		this.Nascondi();
		this.AvviaCorpo0();
		
		var istanza = this;
		var opert = $(this.elementi.str_operazione);
		opert.addEvent('change', function (e) {
										    istanza.Nascondi();
										   	if(this.value=='-1') { new Fx.Scroll(window).toTop(); return;}
										 	if(this.value) istanza.live.contenitore.setStyle('display','block');
											var tilo = $('title_' + this.value);
											tilo.setStyle('display','block');
											var corpo = $('corpo_' + this.value);
											corpo.setStyle('display','block');
											new Fx.Scroll(window).toBottom();
								
											if(this.value=='2') 
												istanza.AvviaCorpo2();
										   });
	}, 
	Nascondi : function () {
		this.live.contenitore.setStyle('display','none');
		var figli  = this.live.contenitore.getElements('h2');
		figli.setStyle('display','none');
		var figli2 = this.live.contenitore.getElements('div');
		figli2.setStyle('display','none');
		

	}, 
	AvviaCorpo0 : function () {
		var saveBtm = $('save_corpo0');
		var selAll = $('selectall');
		var checkBoxs = $$('#corpo_0 table input');
		var myradio = $$('#compressione input');
		
		
		selAll.addEvent('click', function(e) {
										  var checkoolo = this.checked;
										  checkBoxs.each(function (el) {
																if(el.get('id')=='selectall')return;
																el.checked = checkoolo;
																});
										  });
		var istanza = this;
		saveBtm.addEvent('click' , function (e) {
											 var zipMode = '';
											  myradio.each ( function (eel) {
												if(eel.checked) {
													zipMode = eel.value;
													return;
												}
											  });	
											  if ( zipMode == '') zipMode = '2';
												var saveStr = '';
												checkBoxs.each(function (el) { 
													if(el.get('id')=='selectall')return;
													if(el.checked) {
														saveStr+= el.value +',';
													} 
												 });
												
												//appendi un elemento sto elaborando.
												var itemt = new Element('div').set('id','tempBackup');
												itemt.set('html','<img src="/template/images/loading.gif" />');
												var windo =window.getScrollSize();
												itemt.setStyles( {
																  'height':windo.y+'px',
																  'width' :windo.x+'px',
																  'z-index':'999999999',
																  'background':'#FFF',
																  'text-alice':'center',
																  'opacity':0.1
																  } );
												document.body.adopt(itemt);
//												myFirstElement.inject(mySecondElement, 'after');
												itemt.setStyles( {
																  'position':'absolute',
																  'top' :'0px',
																  'left':'0px'
																  } );
												itemt.tween('opacity',0.9);
												istanza.AvviaRichiestaBackup(saveStr,zipMode);		
												//avviare un handler di tipo post con tutti i valori di salvataggio...
												//attenzione analizzare l'output e creare in iframe nascosto con il file da scaricare.
												
											  });
		
		
	},
	
	
	AvviaCorpo2 : function() {
		var container = $('corpo_2');		
		var Spanno = new Element('span');
		container.empty();
		container.adopt(Spanno);
		Spanno.setStyles( { 'display':'block', 'float':'left','margin':'5px', 'padding':'10px', 'border-right':'1px solid black' });
		var req = new Request( {
				method: 'get',
				url: '/versionhandler.ashx',
				onSuccess: function(text,xml) {
					if(!text) {
						Spanno.set('html', 'Nulla');	
						return;
					}
					if(!xml) {
						Spanno.set('html', 'Nulla'  );	
						return;
					}
					
					
					//alert('sono qui:\n' + responseText );
					//Spanno.set('html', text );	
					var testo = '';
					//Analizza i Tag
					var obj = xml.getElementsByTagName('Version');
					if(obj.length>0 ) {
						testo+= 'Versione Locale: <br/>Joack Main Version : ' + obj[0].firstChild.nodeValue +'<br/>';
					}
					var ass_ = xml.getElementsByTagName('Assembly');
					if(ass_.length>0 ) {
						testo +="<dl>";
						for(var i = 0 ; i<ass_.length; i++) {
							var _file = ''; var _ver = ''; 
							for(var j = 0;j<ass_[i].childNodes.length; j++) {
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'name' ) 
									_file = ass_[i].childNodes[j].firstChild.nodeValue;
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'version' ) 
									_ver = ass_[i].childNodes[j].firstChild.nodeValue;
							}
							testo+="<dt>" + _file +"</dt><dd>" + _ver + "</dd>";
						}
					}
					Spanno.set('html', testo) ;
				}
				
		  }).send();
		var spanno2 = new Element ('span');
		container.adopt(spanno2);
		spanno2.setStyles( { 'display':'block', 'float':'left','margin':'5px', 'padding':'10px' });
		container.setStyle('overflow','auto');
		var req = new Request( {
				method: 'get',
				url: '/versionhandler.ashx',
				onSuccess: function(text,xml) {
					if(!text) {
						spanno2.set('html', 'Nulla');	
						return;
					}
					if(!xml) {
						spanno2.set('html', 'Nulla'  );	
						return;
					}
					
					
					//alert('sono qui:\n' + responseText );
					//Spanno.set('html', text );	
					var testo = '';
					//Analizza i Tag
					var obj = xml.getElementsByTagName('Version');
					if(obj.length>0 ) {
						testo+= 'Versione Sul Server Ufficiale: <br/>Joack Main Version : ' + obj[0].firstChild.nodeValue +'<br/>';
					}
					var ass_ = xml.getElementsByTagName('Assembly');
					if(ass_.length>0 ) {
						testo +="<dl>";
						for(var i = 0 ; i<ass_.length; i++) {
							var _file = ''; var _ver = ''; 
							for(var j = 0;j<ass_[i].childNodes.length; j++) {
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'name' ) 
									_file = ass_[i].childNodes[j].firstChild.nodeValue;
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'version' ) 
									_ver = ass_[i].childNodes[j].firstChild.nodeValue;
							}
							testo+="<dt>" + _file +"</dt><dd>" + _ver + "</dd>";
						}
					}
					spanno2.set('html', testo) ;
				}
				
		  }).send();
		
	},
	
	AvviaRichiestaBackup : function (modo, compressione) {
		var istanza = this;
		//step 1: Lettura Elementi (ricevo guid sessione);
		var richiesta = new Request.HTML({
			url:'/administration/core/man_hand.aspx?op=backup',
			method:'post',
			onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				var richiesta = responseHTML;
				alert(richiesta);
				if(richiesta.indexOf('OK')!=-1) {
					alert('Backup Pronto \nsta per partire il download');
					istanza.DownloadFile( richiesta.substring(richiesta.indexOf('~')+1),compressione ); 					
				} else {
					alert('backup non generato.');
					
				}
			}
		}).send('w='+ modo +'&m=' + compressione);
	},
	DownloadFile : function (path,comp) {
		var el = new Element('iframe');
		this.live.contenitore.adopt(el);
		el.set('src','/administration/core/man_hand.aspx?op=download&file='+path + '&comp=' + comp);
		el.setStyle('display','none');
		var tempo = $('tempBackup');
		if(tempo) tempo.destroy();
		
	}
	
});
Manutenzione.implement(new Options, new Events);			


function LoadLockedItems()
{
		var d_elock = $('elock');
		d_elock.empty();
		d_elock.adopt( new Element('h3').set('html','Elementi Bloccati'));
		var ul_s = new Element('ul').addClass('locklist');
		d_elock.adopt(ul_s);
		
		var totaleC =0;
		var li_c = new Element('li').set('html','Contenuti');
		var ul_c = new Element('ul');

		var tempLi = new Element('li');
		tempLi.set('html','Caricamento in corso...');
		ul_s.adopt(tempLi);	
		//contenuti : articoli, categorie, sezioni
		// moduli
		// voci di menu
		var r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('conto');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='0') {
						if(tempLi) tempLi.destroy();
						totaleC++;
						ul_c.adopt(new Element('li').set('html','Contenuti (' + valore + ')') );
					}
				}
			}).send('op=lockcount&source=contenuti');
		r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('conto');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='0') {
						if(tempLi) tempLi.destroy();
						totaleC++;
						ul_c.adopt(new Element('li').set('html','Categorie/Sezioni (' + valore + ')') );
					}
				}
			}).send('op=lockcount&source=contenuti_manager');
			if(totaleC>0 ) {
				li_c.adopt(ul_c);
				ul_s.adopt(li_c);				
			}		
			r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('conto');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='0') {
						if(tempLi) tempLi.destroy();
						totaleC++;
						ul_s.adopt(new Element('li').set('html','Moduli (' + valore + ')') );
					}else {
						ul_s.adopt(new Element('li').set('html','nessun Modulo bloccato') );
					}
				}
			}).send('op=lockcount&source=moduli');	
				
			r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('conto');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='0') {
						if(tempLi) tempLi.destroy();
						totaleC++;					
						ul_s.adopt(new Element('li').set('html','Menu Items (' + valore + ')').setStyle('color','red') );
					} else {
						ul_s.adopt(new Element('li').set('html','nessun Menu Item bloccato') );
					}
				}
			}).send('op=lockcount&source=menu_elemento');					

			r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('conto');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='0') {
						if(tempLi) tempLi.destroy();
						totaleC++;					
						ul_s.adopt(new Element('li').set('html','Blocchi Visivi (' + valore + ')') );
					} else {
						ul_s.adopt(new Element('li').set('html','nessun Blocco Visivo bloccato') );
					}
				}
			}).send('op=lockcount&source=blocchi_visivi');	
			
			if(totaleC==0) {
				ul_s.adopt(new Element('li').set('html','nessun Contenuto bloccato') );
			}		

}

function LoadFilesInfo() {
		//sdir
		var d_sdir = $('sdir');	
		d_sdir.empty();
		d_sdir.adopt( new Element('h3').set('html','Struttura Directory'));
		//controllo scrivibile: bin, controls, template, portale.config
		var ul_s = new Element('dl').addClass('dirlist');
		d_sdir.adopt(ul_s);

		var tempLi = new Element('dl');
		tempLi.set('html','Caricamento in corso...');
		ul_s.adopt(tempLi);		

		//web.config
		var r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('perm');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='') {
						if(tempLi) tempLi.destroy();
						ul_s.adopt(new Element('dt').set('html','/web.config') );
						ul_s.adopt(new Element('dd').set('html',valore) );
					}
				}
			}).send('op=checkfile&source=web.config');
		//portale.config			
		r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('perm');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='') {
						if(tempLi) tempLi.destroy();						
						ul_s.adopt(new Element('dt').set('html','/portale.config') );
						ul_s.adopt(new Element('dd').set('html',valore) );
					}
				}
			}).send('op=checkfile&source=portale.config');
			

		// /bin
		r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('perm');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='') {
						if(tempLi) tempLi.destroy();						
						ul_s.adopt(new Element('dt').set('html','/bin').addClass('dir') );
						ul_s.adopt(new Element('dd').set('html',valore) );
					}
				}
			}).send('op=checkdir&source=bin');
		// /public
		r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('perm');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='') {
						if(tempLi) tempLi.destroy();						
						ul_s.adopt(new Element('dt').set('html','/public').addClass('dir') );
						ul_s.adopt(new Element('dd').set('html',valore) );
					}
				}
			}).send('op=checkdir&source=public');
		// /controls
		r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('perm');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='') {
						if(tempLi) tempLi.destroy();						
						ul_s.adopt(new Element('dt').set('html','/controls').addClass('dir') );
						ul_s.adopt(new Element('dd').set('html',valore) );
					}
				}
			}).send('op=checkdir&source=controls');		
		// /template
		r = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var o = xml.getElementsByTagName('perm');
					if(o.length==0) return;
					var valore = o[0].firstChild.nodeValue;
					if(valore!='0') {
						if(tempLi) tempLi.destroy();						
						ul_s.adopt(new Element('dt').set('html','/template').addClass('dir') );
						ul_s.adopt(new Element('dd').set('html',valore) );
					}
				}
			}).send('op=checkdir&source=template');				
}

function getLogList() {
		//syslog
		var d_slog = $('syslog');
		d_slog.empty();
		d_slog.adopt( new Element('h3').set('html','Log di Sistema'));
		//var fileUL = new Element('ul').addClass('loglist');
		var fileUL = new Element('TABLE').addClass('locklist');
		var t_body = new Element('TBODY');
		var t_head = new Element('THEAD');		
		fileUL.adopt(t_head);		
		var tr_h = new Element('tr');
		var td_h = new Element('td').set('html','Log');
		var td2_h = new Element('td').set('html','last write');
		var tr2_h = new Element('tr');
		var td3_h = new Element('td').set('colspan','2').set('html','path: <b>/public/</b>' );
		t_head.adopt(tr2_h);
		t_head.adopt(tr_h);
		tr_h.adopt(td_h);
		tr_h.adopt(td2_h);		
		tr2_h.adopt(td3_h)		
		fileUL.adopt(t_body);
		d_slog.adopt(fileUL);
		renderFileTable('',t_body,td3_h);
}


function renderFileTable(request,tbody, pather) {
		pather.set('html','path : <b>/public/' + (request==''?'':'log/') + '</b>');													
		if(request!='') {
			if(pather.getElement('pathway') ) 
				$('pathway').destroy();
			if(request!='') {
				var pathMr = new Element('div').set('html','back').set('id','pathway');
				pather.adopt(pathMr );
				pathMr.addEvent('click', function() {
								  if(request=='') return;
								  tbody.empty();
								  renderFileTable('',tbody,pather);
								  });
			}
		}
	
		var tempLi = new Element('td').set('colspan','2');
		tempLi.set('html','Caricamento in corso...');
		tbody.adopt(tempLi);		
		var screen_h = 350;
		var screen_w = 500;				
		if(request=='' && $('pathway')) {
			$('pathway').destroy();	
		}
		var richiesta = request;
		var rDir = new Request ({
				method:'get',
				url   :'/administration/core/home_handler.aspx',
				onSuccess : function (text, xml) {
					if(!xml) return;
					var ass_ = xml.getElementsByTagName('file');
					if(ass_.length==0) {return; }
					tempLi.destroy();
					for(var i = 0; i<ass_.length; i++) {
						var testo = '';
						var _rel = '';
						var _url = '';
						var _date = '';
						var _tipo = '';
						for(var j = 0;j<ass_[i].childNodes.length; j++) {
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'fullname' ) 
									_rel = ass_[i].childNodes[j].firstChild.nodeValue;
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'name' ) 
									_url = ass_[i].childNodes[j].firstChild.nodeValue;
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'data' ) 
									_date = ass_[i].childNodes[j].firstChild.nodeValue;
								if ( ass_[i].childNodes[j].nodeName.toString().toLowerCase() == 'tipo' ) 
									_tipo = ass_[i].childNodes[j].firstChild.nodeValue;
									
						}
						var tir = new Element('tr');
						var td_item = new Element('td').set('html', _url).set('rel',(richiesta==''?_url:('log/'+_url) ) ).setStyle('cursor','pointer');
						var td2_item = new Element('td').set('html', _date).setStyle('cursor','pointer');						
						tir.adopt(td_item);
						tir.adopt(td2_item);
						tbody.adopt( tir );
						if(_tipo!='d' ) 
							td_item.addEvent('click',function(e) {
														  TB_show(this.get('html'), 
																		   '/administration/core/home_handler.aspx?op=readfile&source=/public/'+this.get('rel') +
																		   '&keepThis=true&TB_iframe=true&height='+screen_h+'&width='+screen_w+'','');
														  }
										);
						else {
							td_item.addEvent('click',function(e) {
												//load sons:
												tbody.empty();
												renderFileTable('log',tbody,pather);
											}
										);
						}
						
					}
				}
			}).send('op=getloglist' + (request==''?'':'&source=log'));		
}